package com.heys.demo;


/**
 * 栈：后进先出 没底的  进口和出口是同一个，它是根据放入的先后顺序进行出栈的
 * 栈指针：永远指向栈顶
 *
 * 队列：先进先出
 */
public class StackDemo {

    //根据需要进行论证定义的，但是通常都会定义成2^n
    private int deepTh=16;

    //栈指针
    private int nextPoint = 0;

    //定义栈的基本数据结构
    private int[] stack = new int[deepTh];

    public void press(int data){
        if (nextPoint==deepTh){
            throw new RuntimeException("现在已经到栈顶了，已经不能再放入值了");
        }
        stack[nextPoint] =data;
        nextPoint++;
    }

    public int pop(){
        if (nextPoint==0){
            throw new RuntimeException("现在已经到栈底了，没有值了");
        }
        nextPoint = nextPoint-1;

        int data = stack[nextPoint];
        stack[nextPoint] = 0;
        return data;
    }


    public static void main(String[] args) {
        StackDemo stackDemo = new StackDemo();

        for (int i = 0; i< stackDemo.deepTh;i++){
            stackDemo.press(i);
        }
        for (int i = 0; i< stackDemo.deepTh;i++){
            int data = stackDemo.pop();
            System.out.println(data);
        }

    }
}
